Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

promto

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promto

Create a promise with a timeout.

  • 1.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

promto

npm version

promto Wrappes A Promise In Timeout.

yarn add promto

Table Of Contents

API

The package is available by importing its default function:

import promto from 'promto'

async promto(
  promise: Promise,
  timeout: number,
  description: string?,
): Promise

Creates a new promise which will be rejected upon timeout (after N milliseconds). It will be resolved with the promise value or rejected with the promise error when not timed out.

import promto from 'promto'

/**
 * @param {number} timeout
 * @param {string|Error} result
 */
const makePromise = async (timeout, result) => {
  await new Promise((resolve) => {
    setTimeout(resolve, timeout || 200)
  })
  if (result instanceof Error) throw result
  return result
}

(async () => {
  // ok
  const res = await promto(makePromise(50, 'hello'), 100)
  console.log(res)
  // rejected timeout
  try {
    await promto(makePromise(150, 'world'), 100)
  } catch (err) {
    console.log(err.message)
  }
  // rejected timeout with description
  try {
    await promto(makePromise(150, 'world'), 100, 'Example')
  } catch (err) {
    console.log(err.message)
  }
  // rejected promise
  try {
    await promto(makePromise(50, new Error('Error in promise')), 100)
  } catch (err) {
    console.log(err.message)
  }
})()
hello
Promise has timed out after 100ms
Example has timed out after 100ms
Error in promise

Art Deco © Art Deco 2019 Tech Nation Visa Tech Nation Visa Sucks

Keywords

FAQs

Package last updated on 24 Nov 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc